home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter7 / setpos.c < prev    next >
C/C++ Source or Header  |  1996-04-28  |  11KB  |  316 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "setpos.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLSetPos()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. #define NAME_LEN 20
  108.  
  109. static int   nDeptId;
  110. static UCHAR sDeptName[NAME_LEN+1];
  111. static int   nDeptHeadId;
  112.  
  113. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  114. {
  115. static HENV  hEnv  = NULL;
  116. static HDBC  hDBC  = NULL;
  117. static HWND  hList = NULL;
  118.  
  119. static UCHAR szLBStr[256];
  120.  
  121. UCHAR  szSqlStr[] = "Select dept_id, dept_name, dept_head_id From department";
  122.  
  123.    switch( uMsg )
  124.    {
  125.       case WM_CREATE :
  126.               {
  127.                  RETCODE retCode;
  128.  
  129.                  // Allocate Environment and Connection.
  130.                  //.....................................
  131.                  SQLAllocEnv( &hEnv );
  132.                  SQLAllocConnect( hEnv, &hDBC );
  133.  
  134.                  // Connect to the data source
  135.                  //...........................
  136.                  retCode = SQLConnect( hDBC, 
  137.                              "Test Data Source", SQL_NTS,
  138.                              "DBA",              SQL_NTS,  
  139.                              "SQL",              SQL_NTS );
  140.  
  141.                  if ( retCode != SQL_SUCCESS )
  142.                  {
  143.                     SQLFreeConnect( hDBC );
  144.                     SQLFreeEnv( hEnv );
  145.                     return( -1 );
  146.                  }
  147.  
  148.                  if ( retCode == SQL_SUCCESS )
  149.                     hList = CreateWindow( "LISTBOX", "",    
  150.                                           LBS_NOTIFY | WS_VSCROLL | 
  151.                                           WS_BORDER  | WS_CHILD | 
  152.                                           WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  153.                                           0, 0, 
  154.                                           0, 0,  
  155.                                           hWnd,              
  156.                                           (HMENU)101,              
  157.                                           hInst,         
  158.                                           NULL               
  159.                                         );
  160.               }
  161.               break;
  162.  
  163.       case WM_SIZE :
  164.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  165.                                        HIWORD( lParam ), TRUE );
  166.               break; 
  167.               
  168.       case WM_COMMAND :
  169.               switch( LOWORD( wParam ) )
  170.               {
  171.                  case IDM_TEST :
  172.                      {
  173.                         HSTMT   hStmt;
  174.                         RETCODE retCode;
  175.                         SDWORD  cbValue1 = sizeof(int);
  176.                         SDWORD  cbValue2 = NAME_LEN;
  177.                         SDWORD  cbValue3 = sizeof(int);
  178.                         UDWORD  crow;
  179.                         UWORD   rgfRowStatus;
  180.                         SDWORD  nRowsAdded;
  181.                         
  182.                         // Allocate a statement handle for the query.
  183.                         // ..........................................
  184.                         SQLAllocStmt( hDBC, &hStmt );
  185.                         SQLSetStmtOption( hStmt, SQL_CONCURRENCY, SQL_CONCUR_LOCK );
  186.  
  187.                         // Call SQLExecDirect() to compile
  188.                         // and execute the query.
  189.                         // ...............................
  190.                         retCode = SQLExecDirect( hStmt, szSqlStr, SQL_NTS );
  191.                         
  192.                         if( retCode == SQL_SUCCESS ||
  193.                             retCode == SQL_SUCCESS_WITH_INFO )
  194.                         {
  195.                            // Bind columns.
  196.                            // .............
  197.                            SQLBindCol( hStmt, 1, SQL_C_SLONG, &nDeptId, 
  198.                                        0, &cbValue1 );
  199.                            SQLBindCol( hStmt, 2, SQL_C_CHAR,  sDeptName, 
  200.                                        NAME_LEN, &cbValue2 );
  201.                            SQLBindCol( hStmt, 3, SQL_C_SLONG, &nDeptHeadId, 
  202.                                        0, &cbValue3 );
  203.  
  204.                            // Fetch the records.
  205.                            //...................
  206.                            retCode = SQLExtendedFetch( hStmt, 
  207.                               SQL_FETCH_NEXT, 0, &crow, &rgfRowStatus );
  208.  
  209.                            if ( retCode == SQL_SUCCESS || 
  210.                                 retCode == SQL_NO_DATA_FOUND ||
  211.                                 retCode == SQL_SUCCESS_WITH_INFO )
  212.                            {
  213.                               // assign data
  214.                               //............
  215.  
  216.                               cbValue1 = sizeof(int);
  217.                               cbValue2 = NAME_LEN;   
  218.                               cbValue3 = sizeof(int);
  219.  
  220.                               nDeptId = 600;
  221.                               strcpy(sDeptName, "New Business");
  222.                               nDeptHeadId = 902;
  223.  
  224.                               // Perform an add on the department table.
  225.                               // .......................................
  226.                               retCode = SQLSetPos( hStmt, 1, SQL_ADD,
  227.                                                    SQL_LOCK_NO_CHANGE );
  228.  
  229.                               if( retCode == SQL_SUCCESS )
  230.                               {
  231.                                  SQLTransact( SQL_NULL_HENV, hDBC, SQL_COMMIT );
  232.  
  233.                                  // Determine the number of rows 
  234.                                  // that were updated.
  235.                                  // ............................
  236.                                  SQLRowCount( hStmt, &nRowsAdded );
  237.  
  238.                                  // Report the number of updated 
  239.                                  // columns to the user.
  240.                                  // ............................
  241.                                  szLBStr[0] = '\0';
  242.                                  sprintf( szLBStr, 
  243.                                    "Number of rows added = %ld", nRowsAdded );
  244.  
  245.                                  SendMessage( hList, LB_ADDSTRING, 
  246.                                               0, (LPARAM)szLBStr );
  247.                               }
  248.                               else
  249.                               {
  250.                                  SendMessage( hList, LB_ADDSTRING, 0, 
  251.                                               (LPARAM)"Update Failed" );
  252.  
  253.                               }
  254.                            }
  255.                         }
  256.  
  257.                         // Free the statement handle.
  258.                         // ..........................
  259.                         SQLFreeStmt( hStmt, SQL_DROP );
  260.                      }
  261.                      break;
  262.  
  263.                  case IDM_ABOUT :
  264.                      DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  265.                      break;
  266.  
  267.                  case IDM_EXIT :
  268.                      DestroyWindow( hWnd );
  269.                      break;
  270.               }
  271.               break;
  272.       
  273.       case WM_DESTROY :
  274.               PostQuitMessage(0);
  275.  
  276.               // Disconnect Environment.
  277.               //........................
  278.               SQLDisconnect( hDBC );
  279.  
  280.               // Free Connection and Environment.
  281.               //.................................
  282.               SQLFreeConnect( hDBC );
  283.               SQLFreeEnv( hEnv );
  284.               break;
  285.  
  286.       default :
  287.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  288.    }
  289.  
  290.    return( 0L );
  291. }
  292.  
  293.  
  294. LRESULT CALLBACK About( HWND hDlg,           
  295.                         UINT message,        
  296.                         WPARAM wParam,       
  297.                         LPARAM lParam)
  298. {
  299.    switch (message) 
  300.    {
  301.        case WM_INITDIALOG: 
  302.                return (TRUE);
  303.  
  304.        case WM_COMMAND:                              
  305.                if (   LOWORD(wParam) == IDOK         
  306.                    || LOWORD(wParam) == IDCANCEL)    
  307.                {
  308.                        EndDialog(hDlg, TRUE);        
  309.                        return (TRUE);
  310.                }
  311.                break;
  312.    }
  313.  
  314.    return (FALSE); 
  315. }
  316.